package jmine.tec.hydra.domain.filter;

import java.util.List;

import jmine.tec.hydra.api.HydraMessage;
import jmine.tec.hydra.api.HydraSpecification;
import jmine.tec.hydra.dao.filter.HydraFilterDAO;

/**
 * Serviço responsável por aplicar filtros a uma mensagem.
 * 
 * @author marco.vieira
 */
public class HydraFilterService {
    private final HydraFilterDAO filterDAO;

    private final HydraMessage message;

    private final HydraSpecification specification;

    private final HydraFilterType filterType;

    /**
     * @param message mensagem.
     * @param specification especificação.
     * @param filterType tipo do filtro.
     */
    public HydraFilterService(HydraMessage message, HydraSpecification specification, HydraFilterType filterType) {
        this.message = message;
        this.specification = specification;
        this.filterType = filterType;
        this.filterDAO = message.getController().getDAOFactory().getDAOByClass(HydraFilterDAO.class);
    }

    /**
     * Aplica os filtros a mensagem.
     */
    public void applyFilters() {
        List<HydraFilter> filters = this.findFilters();
        for (HydraFilter filter : filters) {
            filter.execute(this.message);
        }
    }

    /**
     * @return lista de filtros.
     */
    private List<HydraFilter> findFilters() {
        if (this.filterType.equals(HydraFilterType.SENDING)) {
            return this.filterDAO.findSendingBySpecification(this.specification);
        }
        return this.filterDAO.findReceivingBySpecification(this.specification);
    }
}
